package trungnt.wipeoff;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;

import trungnt.wipeoff.enums.LoadingEnum;
import trungnt.wipeoff.model.GameState;
import trungnt.wipeoff.model.MyStage;

public class LoadingState extends GameState {

	// private GameScreen _game = null;
	private AssetManager _as = null;
	private SpriteBatch _sb = null;

	private BitmapFont loadingFont = null;
	private LoadingEnum loadingState = null;
	private Vector2 loadingLocation = null;

	public LoadingState(GameScreen game, Stage stage) {
		super(game, null);
		// this._game = game;
		this._as = this.getGame().getAssetManager();
		this._sb = this.getGame().getSpriteBatch();

		this.loadingState = LoadingEnum.SHOW;

		this.loadingFont = this._as.get("fonts/bmp/diavlo_black_72.fnt", BitmapFont.class);
		this.loadingFont.setColor(Color.WHITE);
	}

	@Override
	public void render(float delta) {
		// Gdx.app.log("TrungNT", "Loading state rendering...");

		switch (this.loadingState) {
			case SHOW:
				if (loadingLocation == null) {
					TextBounds tb = this.loadingFont.getBounds("LOADING...");
					loadingLocation = new Vector2(Cs.VW / 2 - tb.width / 2, Cs.VH / 2 + tb.height);
				}

				this._sb.begin();
				this.loadingFont.draw(this._sb, "LOADING...", loadingLocation.x, loadingLocation.y);
				this._sb.end();

				this.loadingState = LoadingEnum.LOADING;

				Gdx.graphics.requestRendering();

				break;
			case LOADING:

				this.loadData();
				this._as.finishLoading();
				this.loadingState = LoadingEnum.LOADED;
				Gdx.graphics.requestRendering();

				break;

			case LOADED:
				// loadingLocation.y -= delta*1200;

				// this._sb.begin();
				// this.loadingFont.draw(this._sb, "LOADING...",
				// loadingLocation.x, loadingLocation.y);
				// this._sb.end();

				// if(loadingLocation.y <=0)
				// {
				this.getGame().changeState(new MenuState(this.getGame(), new MyStage()));
				Gdx.graphics.requestRendering();
				this.getGame().unloadState();
				// }

				return;
		}
	}

	private void loadData() {
		this._as.load("images/bg.png", Texture.class);
		this._as.load("images/bg1.png", Texture.class);
		this._as.load("images/bg2.png", Texture.class);

		this._as.load("images/number1.png", Texture.class);
		this._as.load("images/number2.png", Texture.class);
		this._as.load("images/number3.png", Texture.class);
		this._as.load("images/number4.png", Texture.class);
		this._as.load("images/number5.png", Texture.class);
		this._as.load("images/number6.png", Texture.class);
		this._as.load("images/number7.png", Texture.class);
		this._as.load("images/number8.png", Texture.class);
		this._as.load("images/number9.png", Texture.class);
		this._as.load("images/gameoverbg.png", Texture.class);
		this._as.load("fonts/bmp/diavlo_black_30.fnt", BitmapFont.class);
		this._as.load("fonts/bmp/diavlo_book_36.fnt", BitmapFont.class);
		this._as.load("fonts/bmp/diavlo_bold_60.fnt", BitmapFont.class);
	}

	@Override
	public void dispose() {
		this.loadingFont.dispose();

		loadingFont = null;
		loadingState = null;
		loadingLocation = null;
	}
}
